package cn.lili.controller.promotion;

import cn.lili.common.enums.PromotionTypeEnum;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.OperationalJudgment;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.AuthUserFieldEnum;
import cn.lili.common.security.enums.ObjectFieldEnum;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.promotion.entity.dos.Pintuan;
import cn.lili.modules.promotion.entity.dos.PromotionGoods;
import cn.lili.modules.promotion.entity.dto.search.PintuanSearchParams;
import cn.lili.modules.promotion.entity.dto.search.PromotionGoodsSearchParams;
import cn.lili.modules.promotion.entity.enums.PromotionsStatusEnum;
import cn.lili.modules.promotion.entity.vos.PintuanMemberVO;
import cn.lili.modules.promotion.entity.vos.PintuanShareVO;
import cn.lili.modules.promotion.entity.vos.PintuanVO;
import cn.lili.modules.promotion.entity.vos.SeckillGoodsVO;
import cn.lili.modules.promotion.service.PintuanService;
import cn.lili.modules.promotion.service.PromotionGoodsService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 平台拼团接口
 *
 * @author paulG
 * @since 2020/10/9
 **/
@RestController
@Api(tags = "平台拼团接口")
@RequestMapping("/promotion/pintuan")
@RequiredArgsConstructor
public class PintuanController {

    private final PintuanService pintuanService;

    private final PromotionGoodsService promotionGoodsService;

    private static final String STORE_ID = "storeId";


    @GetMapping(value = "/{id}")
    @ApiOperation(value = "通过id获取")
    public ResultMessage<PintuanVO> get(@PathVariable String id, PintuanSearchParams queryParam) {
        queryParam.setId(id);
        PintuanVO pintuan = pintuanService.getPintuanVO(queryParam);
        return ResultUtil.data(pintuan);
    }

    @GetMapping
    @ApiOperation(value = "根据条件分页查询拼团活动列表")
    public ResultMessage<Page<Pintuan>> getPintuanByPage(PintuanSearchParams queryParam, PageVO pageVo) {
        Page<Pintuan> pintuanIPage = pintuanService.pageFindAll(queryParam, pageVo);
        return ResultUtil.data(pintuanIPage);
    }

    @GetMapping("/goods/{pintuanId}")
    @ApiOperation(value = "根据条件分页查询拼团活动商品列表")
    public ResultMessage<Page<PromotionGoods>> getPintuanGoodsByPage(@PathVariable String pintuanId, PageVO pageVo) {
        PromotionGoodsSearchParams searchParams = new PromotionGoodsSearchParams();
        searchParams.setPromotionId(pintuanId);
        searchParams.setPromotionType(PromotionTypeEnum.PINTUAN.name());
        return ResultUtil.data(promotionGoodsService.pageFindAllByPinTuan(searchParams, pageVo));
    }

    @ApiOperation(value = "获取拼团商品")
    @GetMapping("/goods")
    public ResultMessage<Page<PromotionGoods>> getPintuanCategory(String goodsName, String categoryPath, PageVO pageVo) {
        PromotionGoodsSearchParams searchParams = new PromotionGoodsSearchParams();
        searchParams.setGoodsName(goodsName);
        searchParams.setPromotionType(PromotionTypeEnum.PINTUAN.name());
        searchParams.setPromotionStatus(PromotionsStatusEnum.START.name());
        searchParams.setCategoryPath(categoryPath);
        return ResultUtil.data(promotionGoodsService.pageFindAllByPinTuan(searchParams, pageVo));
    }

    @PostMapping
    @ApiOperation(value = "添加拼团活动")
    public ResultMessage<String> addPintuan(@RequestBody @Validated PintuanVO pintuan) {
        AuthUser authUser = Objects.requireNonNull(UserContext.getCurrentUser());
        pintuan.setStoreId(authUser.getExtendId());
        pintuan.setStoreName(authUser.getExtendName());
        if (pintuanService.savePromotions(pintuan)) {
            return ResultUtil.success();
        }
        throw new ServiceException(ResultCode.PINTUAN_ADD_ERROR);
    }

    @PutMapping
    @ApiOperation(value = "修改拼团活动")
    public ResultMessage<String> editPintuan(@RequestBody @Validated PintuanVO pintuan) {
        OperationalJudgment.judgment(pintuanService.getById(pintuan.getId()), ObjectFieldEnum.STORE_ID, AuthUserFieldEnum.EXTEND_ID);
        AuthUser authUser = Objects.requireNonNull(UserContext.getCurrentUser());
        pintuan.setStoreId(authUser.getExtendId());
        pintuan.setStoreName(authUser.getExtendName());
        if (pintuanService.updatePromotions(pintuan)) {
            return ResultUtil.success();
        }
        throw new ServiceException(ResultCode.PINTUAN_EDIT_ERROR);
    }

    @DeleteMapping("/{pintuanId}")
    @ApiOperation(value = "手动删除拼团活动")
    public ResultMessage<String> deletePintuan(@PathVariable String pintuanId) {
        OperationalJudgment.judgment(pintuanService.getById(pintuanId), ObjectFieldEnum.STORE_ID, AuthUserFieldEnum.EXTEND_ID);
        if (pintuanService.removePromotions(Collections.singletonList(pintuanId))) {
            return ResultUtil.success();
        }
        throw new ServiceException(ResultCode.PINTUAN_DELETE_ERROR);
    }

    @PutMapping("/status/{pintuanIds}")
    @ApiOperation(value = "操作拼团活动状态")
    public ResultMessage<String> openPintuan(@PathVariable String pintuanIds, Long startTime, Long endTime) {
        if (pintuanService.updateStatus(Arrays.asList(pintuanIds.split(",")), startTime, endTime)) {
            return ResultUtil.success();
        }
        throw new ServiceException(ResultCode.PINTUAN_MANUAL_OPEN_ERROR);
    }

    @ApiOperation(value = "获取当前拼团活动的未成团的会员")
    @GetMapping("/{pintuanId}/members")
    public ResultMessage<List<PintuanMemberVO>> getPintuanMember(@PathVariable String pintuanId,String skuId) {
        return ResultUtil.data(pintuanService.getPintuanMember(pintuanId, skuId));
    }

    @ApiOperation(value = "获取当前拼团订单的拼团分享信息")
    @GetMapping("/share")
    public ResultMessage<PintuanShareVO> share(String parentOrderSn, String skuId) {
        return ResultUtil.data(pintuanService.getPintuanShareInfo(parentOrderSn, skuId));
    }

    @ApiOperation(value = "根据goodsId获取拼团商品sku")
    @GetMapping("/getPintuanGoodsSkuList/{pintuanId}/{goodsId}")
    public ResultMessage<List<PromotionGoods>> getPintuanGoodsSkuList (@PathVariable String pintuanId, @PathVariable String goodsId) {
        return ResultUtil.data(pintuanService.getPintuanGoodsSkuList(pintuanId, goodsId));
    }

    @ApiOperation(value = "删除拼团商品")
    @GetMapping("/deletePinTuanGoods/{pintuanId}/{goodsId}")
    public ResultMessage<Object> deletePinTuanGoods (@PathVariable String pintuanId, @PathVariable String goodsId) {
        pintuanService.deletePinTuanGoods(pintuanId, goodsId);
        return ResultUtil.success();
    }

}
